package com.xingdata.jjxc.m.avt;

import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.RequestCode;
import com.xingdata.jjxc.adapter.CommAddressAdp;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.base.BaseActivity;
import com.xingdata.jjxc.enty.ComAddressEntity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.map.navi.activity.NaviLocationActivity;
import com.xingdata.jjxc.utils.BluetoothTools;
import com.xingdata.jjxc.utils.Code;
import com.xingdata.jjxc.utils.Constants;
import com.xingdata.jjxc.utils.JUtils;
import com.xingdata.jjxc.utils.SP;
import com.xingdata.jjxc.utils.SystemInfo;
import com.xingdata.jjxc.views.CalculateHeightListView;
import com.xingdata.jjxc.views.Widget;

/**
 * 常用地点主页--1：设置 2：导航
 * 
 * @author maomao
 * 
 */
public class Commonaddavt extends Base1Activity implements OnClickListener, OnItemClickListener {

	/** 添加常用地点 */
	private RelativeLayout comm_add_place_rl;

	private ComAddressEntity commEntity;

	private List<ComAddressEntity> comEnList;
	private CommAddressAdp addressAdp;
	private CalculateHeightListView common_address_clv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// ExitApplication.getInstance().addActivity(this);
		// ActivityTools.addActivities(this);
		doPost_siteList();
	}

	@Override
	protected int getContentViewId() {
		return R.layout.activity_commonaddavt;
	}

	@Override
	protected String getTitleText() {
		return JUtils.TITILE_COMMADD;
	}

	@Override
	protected void initViews() {
		comm_add_place_rl = (RelativeLayout) findViewById(R.id.comm_add_place_rl);
		comm_add_place_rl.setOnClickListener(this);

		common_address_clv = (CalculateHeightListView) findViewById(R.id.common_address_clv);
		common_address_clv.setOnItemClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.comm_add_place_rl:
//			Intent intent = new Intent(Commonaddavt.this, NaviLocationActivity.class);
//			Bundle bundle = new Bundle();
//			bundle.putInt("inNaviLocionType", Constants.inNaviLocionType_COLLECTION);
//			intent.putExtras(bundle);
//			Bluetooth.bluetoothConnectd(Commonaddavt.this, intent);
//			startActivity(new Intent(Commonaddavt.this, ComAddressMapavt.class));
			
			
			break;

		default:
			break;
		}
	}

	@Override
	public void onResume() {

		doPost_siteList();
		super.onResume();
	}

	private void doPost_siteList() {
		if (isNetworkConnected() == false) {
			showToast("网络连接断开，请检查网络");
			return;
		}
		paramsMap = new HashMap<String, String>();
		paramsMap.put("access_token", SystemInfo.getUserInfo().getToken());
		paramsMap.put("userid", SystemInfo.getUserInfo().getUserid());
		paramsMap.put("beginnum", "0");
		paramsMap.put("endnum", "10000");
		mHttpUtil.Post(App.ZZD_REQUEST_SITELIST, paramsMap, new HttpResponseListener() {

			@Override
			public void onSuccess(RespEntity respEntity) {
				System.out.println("aaaa");
			}

			@Override
			public void onLoading(int vurrent) {

			}

			@Override
			public void onFailure(String result) {
				// TODO Auto-generated method stub

			}
		});

		// resp = null;
		// params = new AbRequestParams();
		// // params.put("access_token",
		// Code.accessToken(SystemInfo.getUserInfo()
		// // .getMobile(), SystemInfo.getUserInfo().getPw(), SystemInfo
		// // .getUserInfo().getUserid()));
		// params.put("access_token", Code.accessToken());
		// params.put("userid", SystemInfo.getUserInfo().getUserid());
		// params.put("beginnum", "0");
		// params.put("endnum", "10000");
		//
		// mAbHttpUtil.post(App.ZZD_REQUEST_SITELIST, params,
		// new AbStringHttpResponseListener() {
		//
		// @Override
		// public void onSuccess(int statusCode, String content) {
		// resp = JSON.parseObject(content, RespEntity.class);
		// Log.i("LOGIN", content);
		// }
		//
		// // 开始执行前
		// @Override
		// public void onStart() {
		// showProgressDialog("加载中，请稍等");
		// }
		//
		// // 失败，调用
		// @Override
		// public void onFailure(int statusCode, String content,
		// Throwable error) {
		// Log.i("LOG", error.getMessage());
		// }
		//
		// // 完成后调用，失败，成功
		// @Override
		// public void onFinish() {
		// if (resp != null) {
		// if (resp.getState() == 0) {
		// comEnList = JUtils.getSiteList(resp
		// .getSitelist());
		// addressAdp = new CommAddressAdp(
		// Commonaddavt.this, comEnList);
		// common_address_clv.setAdapter(addressAdp);
		// SP.saveSite(Commonaddavt.this,
		// resp.getSitelist());
		// } else if (resp.getState() == 1) {
		// showToast(resp.getMsg());
		// } else {
		// showToast(resp.getMsg());
		// }
		// removeProgressDialog();
		// } else {
		// showToast("网络超时，请重试");
		// removeProgressDialog();
		// return;
		// }
		//
		// };
		//
		// });
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// if (isNetworkConnected() == false) {
		// showToast("网络连接断开，请检查网络");
		// return;
		// }
		commEntity = comEnList.get(position);
		ListView lv = (ListView) parent;
		Bundle bundle = getIntent().getExtras();
		if (bundle == null) {
			bundle = new Bundle();
		}

		Intent intent = new Intent();
		// bundle.putInt("inNaviLocionType",
		// BluetoothTools.inNaviLocionType_COLLECTION);
		// 10 11 表示 列表中“家”和“公司”
		if ("10".equals(commEntity.getSite_id()) || "11".equals(commEntity.getSite_id())) {
			// intent.setClass(Commonaddavt.this,
			// ComAddressMapavt.class);
		} else {
			// intent.setClass(Commonaddavt.this,
			// ComAddressListavt.class);
			bundle.putSerializable("state_anv", "2");// 从首页或者是常用地址到导航页面的状态值

		}
		intent.setClass(Commonaddavt.this, NaviLocationActivity.class);
		bundle.putInt("inNaviLocionType", Constants.inNaviLocionType_COMMON);
		bundle.putSerializable("ComAddressEntity", commEntity);// 结束纬度
		intent.putExtras(bundle);
		// Bluetooth.bluetoothConnectd(Commonaddavt.this, intent);
		// if (BluetoothGattTools.getUartService() ==
		// null||!SystemInfo.isConnect) {
		// Bundle mBundle = intent.getExtras();
		// intent= new Intent(this,
		// BluetoothListActivity.class);
		// intent.putExtras(mBundle);
		// startActivityForResult(intent, RequestCode.RequestCode_BlueListView);
		// }else {
		// startActivity(intent);
		// }

		Widget.startActivityAnim(Commonaddavt.this, R.anim.avty_slide_in_from_right, R.anim.avty_slide_out_from_left);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode != RESULT_OK) {
			return;
		}
		if (requestCode == RequestCode.RequestCode_BlueListView) {
			setResult(RESULT_OK, data);
			finish();
		}

	}

	// public boolean onKeyDown(int keyCode, KeyEvent event) {
	//
	// // 如果是返回键,直接返回到桌面
	// if (keyCode == KeyEvent.KEYCODE_BACK) {
	// setResult(RESULT_OK);
	// finish();
	// }
	// return super.onKeyDown(keyCode, event);
	//
	// }
}
